<template>
	<div class="system-user-dialog-container">
		<el-dialog
			:close-on-click-modal="false"
			:title="dataForm.userId ? $t('common.editBtn') : $t('common.addBtn')"
			draggable
			v-model="visible"
			v-if="visible"
			width="600"
			@close="onClose"
		>
			<el-form :model="dataForm" :rules="dataRules" label-width="90px" ref="dataFormRef" v-loading="loading">
				<el-row :gutter="20">
					<el-col :span="12" class="mb-6">
						<el-form-item :label="$t('pickApply.designColor')" prop="materialCode">
							<designColorSelect v-model="dataForm.materialCode" res="designColorRef" :placeholder="$t('pickApply.pleaseSelect')"> </designColorSelect>
						</el-form-item>
					</el-col>
					<el-col :span="12" class="mb-6">
						<el-form-item :label="$t('pickApply.trunks')" prop="trunks">
							<el-input clearable v-model="dataForm.trunks" type="number"></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12" class="mb-6">
						<el-form-item :label="$t('pickApply.planDateStart')" prop="planDateStart">
							<el-date-picker v-model="dataForm.planDateStart" type="date" style="width: 100%" @change="planDateChange" />
						</el-form-item>
					</el-col>

					<el-col :span="12" class="mb-6">
						<el-form-item :label="$t('pickApply.planDateEnd')" prop="planDateEnd">
							<el-date-picker v-model="dataForm.planDateEnd" type="date" style="width: 100%" @change="planDateChange" />
						</el-form-item>
					</el-col>

					<el-col :span="12" class="mb-6">
						<el-form-item :label="$t('pickApply.planDays')" prop="planDays">
							<el-input clearable v-model="dataForm.planDays" type="number" disabled></el-input>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onClose" icon="close">{{ $t('common.cancelButtonText') }}</el-button>
					<el-button @click="onSubmit" type="primary" icon="check" :disabled="loading">{{ $t('common.confirmButtonText') }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script lang="ts" name="spDialog" setup>
import { useMessage } from '/@/hooks/message';
import { insertPickApply } from '/@/api/platform/wms/pickApply';
import designColorSelect from '/@/components/mes/select/DesignColorSelect.vue';
import { useI18n } from 'vue-i18n';

const { t } = useI18n();
// 定义变量内容
const dataFormRef = ref();
const visible = ref(false);
const loading = ref(false);

// 通知父组件刷新
const emit = defineEmits(['refresh']);

const dataForm = reactive({
	barcode: '',
	materialId: '',
	locationId: '',
	Qty: null,
	materialCode: '',
  trunks: null,
  planDateStart: '',
  planDateEnd: '',
  planDays: null,
});
const designColorRef = ref();
const dataRules = ref({
	materialCode: [{ required: true, message: t('bar.cannotEmpty'), trigger: 'blur' }],
	planDateStart: [{ required: true, message: t('bar.cannotEmpty'), trigger: 'blur' }],
	planDateEnd: [{ required: true, message: t('bar.cannotEmpty'), trigger: 'blur' }],
	trunks: [{ required: true, message: t('bar.cannotEmpty'), trigger: 'blur' }],
	planDays: [{ required: true, message: t('bar.cannotEmpty'), trigger: 'blur' }],
});
// 打开弹窗
const openDialog = async (id: string) => {
	visible.value = true;
	// 新增清空form表单的数据
	Object.assign(dataForm, {
    barcode: '',
    materialId: '',
    locationId: '',
    Qty: null,
    materialCode: '',
    trunks: null,
    planDateStart: '',
    planDateEnd: '',
    planDays: null,
  });
	// 重置表单数据
	nextTick(() => {
		dataFormRef.value?.resetFields();
	});
};

const onClose = () => {
	visible.value = false;
	dataForm.materialCode = '';
	dataFormRef.value?.resetFields();
	designColorRef.value?.clear();
};
//重新选日期自动计算相差天数
const planDateChange = () => {
	// 时间不存在就返回不往下面执行
	if (!dataForm.planDateStart) return;
	if (!dataForm.planDateEnd) return;
	if (dataForm.planDateEnd.getTime() < dataForm.planDateStart.getTime()) {
		dataForm.planDays = '';
		return;
	}
	var days = getDaysDiff(dataForm.planDateStart, dataForm.planDateEnd);
	dataForm.planDays = days + 1;
};

function getDaysDiff(startDate, endDate) {
	const oneDay = 24 * 60 * 60 * 1000; // 一天的毫秒数
	const start = new Date(startDate);
	const end = new Date(endDate);

	let diffDays = Math.floor(Math.abs(end.getTime() - start.getTime()) / oneDay);
	return diffDays;
}

// 提交
const onSubmit = async () => {
	const valid = await dataFormRef.value.validate().catch(() => {});
	if (!valid) return false;

	try {
		loading.value = true;
		await insertPickApply(dataForm).then((res) => {
			if (!res.ok) {
				useMessage().error(res.data);
			}
		});
		visible.value = false; // 关闭弹窗
		emit('refresh');
	} catch (error: any) {
		useMessage().error(error.msg);
	} finally {
		loading.value = false;
	}
};

// 暴露变量
defineExpose({
	openDialog,
});
</script>
